package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.Company;
import com.zygh.policeasset.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/company"})
@Api(tags = {"单位/公司管理"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/CompanyController.class */
public class CompanyController {
    @Autowired
    CompanyService companyService;

    @RequestMapping(value = {"/findall"}, method = {RequestMethod.GET})
    @ApiOperation("获取全部单位/公司列表")
    public Object findAll() {
        return this.companyService.findAll();
    }

    @RequestMapping(value = {"/findbycp"}, method = {RequestMethod.GET})
    @ApiOperation("获取全部公司列表")
    public Object findByCp() {
        return this.companyService.findByCp();
    }

    @RequestMapping(value = {"/findbyid"}, method = {RequestMethod.GET})
    @ApiOperation("获取单位/公司名称")
    public Object findById(@RequestParam(value = "id", required = false, defaultValue = "1") Integer id) {
        return this.companyService.findById(id);
    }

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    @ApiOperation("获取全部单位/公司分页列表")
    public Object findAll(@RequestParam String companyName, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        return this.companyService.findAll(companyName, pageNum, pageSize);
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加单位/公司")
    public Object add(@RequestBody Company company) {
        if (StrUtil.isEmpty(company.getCompanyName())) {
            return new CommonResult().validateFailed("名称不能为空");
        }
        int res = this.companyService.add(company).intValue();
        if (res == -1) {
            return new CommonResult().validateFailed("名称不能重复");
        }
        if (res > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("修改单位/公司")
    public Object update(@RequestBody Company company) {
        if (StrUtil.isEmpty(company.getCompanyName())) {
            return new CommonResult().validateFailed("名称不能为空");
        }
        int res = this.companyService.update(company).intValue();
        if (res == -1) {
            return new CommonResult().validateFailed("名称不能重复");
        }
        if (res > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改错误");
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.GET})
    @ApiOperation("删除单位")
    public Object remove(@RequestParam int id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.companyService.remove(id).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }
}